// pages/leftSlide/leftSlide.js

const App = getApp()
const WXAPI = require('../../wxapi/main')
const Touches = require('./../../utils/Touches.js')

Page({
  data: {
    itemData: [],
  },
  touchS: function (e) {  // touchstart
    let startX = Touches.getClientX(e)
    startX && this.setData({ startX })
  },
  touchM: function (e) {  // touchmove
    let itemData = Touches.touchM(e, this.data.itemData, this.data.startX)
    itemData && this.setData({ itemData })

  },
  touchE: function (e) {  // touchend
    const width = 150  // 定义操作列表宽度
    let itemData = Touches.touchE(e, this.data.itemData, this.data.startX, width)
    itemData && this.setData({ itemData })
  },
  // add
  toAdd: function() {
    wx.navigateTo({
      url: '/pages/my-network/add',
    })
  },
  // del
  toDel: function(e) {
    console.log('to del ', e)
    let that = this
    let postData = {id: e.target.dataset.index}
    console.log('exec to del ', postData)
    WXAPI.delNetwork(postData)
    .then(res => {
      console.log('del res ', res)
      if (res.code === 0) {
        that.onShow()
      } else {
        wx.showToast({
          title: '异常',
          icon: 'error'
        })
      }
    })
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    let that = this
    let skey = wx.getStorageSync('loginFlag')
    if (skey) {
      WXAPI.loadNetworkList({skey})
      .then(res => {
        console.log('res ', res)
        if (res.code === 0) {
          that.setData({
            itemData: res.data
          })
        }
      })
    }
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})